import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import Main from '../views/main/index.vue'
import { LocalCache } from "@/utils/localCache"
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect:"/main",
    
  },
  {
    path:"/main",
    component:Main,
    children:[
      {
        path:"scriptUpload",
        // redirect:"/scriptUpload",
        component:() => import(/* webpackChunkName: "about" */ '../views/scriptUpload/index.vue')
      }
    ]
  },
  // {
  //   path: '/',
  //   redirect:"/scriptUpload",
  //   name: 'Home',
  // },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/login/index.vue')
  },
  // {
  //   path: '/scriptUpload',
  //   name: 'scriptUpload',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/scriptUpload/index.vue')
  // }
]




const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  console.log("to", to);
  if(to.path !== "/login") {
    console.log("不等于");
    
      const value = LocalCache.getItem("userInfo")?.token;
      console.log("value:", value);
      
      if(!value) {
        console.log("没有");
        
        router.push("/login")
        return
      }
  }
  // return
  next()
  
})


export default router
